﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using Social.Core.Contracts;
 
namespace Social.Core
{
    /// <summary>
    /// provides means to deserialize a json string
    /// </summary>
    public class JsDeserializer : IDeserializer
    {
        /// <summary>
        /// Deserializes the json string
        /// </summary>
        /// <param name="json">string to deserialize</param>
        /// <param name="converter">in case the user especify a converter it will be used to deserialize the json string</param>
        /// <returns></returns>
        public dynamic Deserialize(string json, JavaScriptConverter[] converter)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();

            if (converter != null)
                jss.RegisterConverters(converter);

            dynamic objectResult = jss.Deserialize(json, typeof(object)) as dynamic;

            return objectResult;
        }

    }
}
